package xyz.yuzegod.megawalls.classes.normal.squid;

import net.citizensnpcs.api.CitizensAPI;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import xyz.yuzegod.megawalls.classes.*;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.util.ItemBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Squid extends Classes
{
    public static final Map<GamePlayer, Integer> skillCooldown;
    public static final Map<GamePlayer, Integer> skill3Cooldown;
    
    public Squid() {
        super("Shark", "鱿鱼", ChatColor.BLUE, Material.INK_SACK, (byte)0, ClassesType.NORMAL, new ClassesInfo.Orientation[] { ClassesInfo.Orientation.TANK, ClassesInfo.Orientation.CONTROL }, ClassesInfo.Difficulty.ONE);
        this.setMainSkill(new MainSkill(this));
        this.setSecondSkill(new SecondSkill(this));
        this.setThirdSkill(new ThirdSkill(this));
        this.setCollectSkill(new FourthSkill(this));
        this.setDefaultSkin("eyJ0aW1lc3RhbXAiOjE1NDk0MzQ0NzE2NzksInByb2ZpbGVJZCI6ImVmYWY1NzU3NzgxZTQ3YWViYzE0Y2Q4MmM5MWM3ZjgyIiwicHJvZmlsZU5hbWUiOiJNaW5lU2tpbiIsInNpZ25hdHVyZVJlcXVpcmVkIjp0cnVlLCJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvM2NhZmVkZTU5MTZlM2FlOTVjMzY2ZGE4NWZmMmI3NTQ5ZjA3NzMzZTg5OTIxNzkxNjA5ZGU5MThmZTI2YTk0ZCJ9fX0=", "s86KHFEFFj90dD02apLFVAOX5mIjggI+mDiEedQhBaFxh/qB4dJPFMjl/l7X92RCmtsWD/XlcxLZCiKYX7i5Et/Xz+HN0ovxrrv6wTGCpNl5BfcFPgq1QkE31FvvPZowhcNoTAL5L8sq9pW2MQdvNE54PaJTl1Usd9otd4atsZHVOkHjSK0KkZ7HxE7sSn8CFXU7G1GQGxpk4WannwPv5sxdqeRKmdqEW/GgZPnH2C+Z0n648GJCKaea76U5NeaIWxGdY2ISEc8DBC9qUH0Q4AWmcn7S8GyNnSH85H0JVwiqIfZjGqTCjq32oJ8jN5tT4s8qeJppL9yg+JlEZ34fDxqHyrK2X1lDz91mRE4rjGS3V0uvOvJkOCkIjS2bJ8tGoh9pfKHLFtqrOgEBoVfRZOZnKw1UGfxN/UV++990m4C30uLxUuGzoY24vAGoyxW4fIqnqZdIugDiEZ8abVmAuhvtnSFUMO3DFGERVABnXSvZentCz4N6p7PWH+d4M1+6n2xcg4u/clxU0sJKW8wvw2RMg4ETlPUbf3I4brGnJAbkGOQKHPdy56OI8Y7nUDpTLztCSEvvEj/pqV+vX+W8mdtRivlRDwHlSgKxIOYgUooGSeKcDkP2i+eFD1BZrdNN1vB3IrQQtfJAtTU15624q6W5SCRnCLrhrvSe7Kd5mDE=");
        this.setEquipmentPackage(new EquipmentPackage(this) {
            @Override
            public List<ItemStack> getEquipments(final int level) {
                final List<ItemStack> items = new ArrayList<ItemStack>();
                items.add(new ItemBuilder(Material.COMPASS).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 指南针").build());
                items.add(new ItemBuilder(Material.DIAMOND_PICKAXE).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 镐").setUnbreakable(true).addEnchantment(Enchantment.DIG_SPEED, 3).addEnchantment(Enchantment.DURABILITY, 3).build());
                items.add(new ItemBuilder(Material.ENDER_CHEST).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 末影箱").build());
                switch (level) {
                    case 1: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.IRON_BOOTS).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 靴子").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1).build());
                        items.add(new ItemBuilder(Material.COOKED_FISH, 2).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 烤制 鲑鱼").build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        break;
                    }
                    case 2: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.IRON_BOOTS).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 靴子").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2).build());
                        items.add(new ItemBuilder(Material.COOKED_FISH, 4).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 烤制 鲑鱼").build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)5).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        break;
                    }
                    case 3: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.IRON_BOOTS).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 靴子").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 3).build());
                        items.add(new ItemBuilder(Material.COOKED_FISH, 4).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 烤制 鲑鱼").build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)5).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)2).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
                    }
                    case 4: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.IRON_BOOTS).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 靴子").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 4).addEnchantment(Enchantment.DEPTH_STRIDER, 1).build());
                        items.add(new ItemBuilder(Material.COOKED_FISH, 6).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 烤制 鲑鱼").build());
                        items.add(new ItemBuilder(Material.POTION, 3, (byte)5).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)2).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
                    }
                    case 5: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.DIAMOND_BOOTS).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 靴子").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 3).addEnchantment(Enchantment.DEPTH_STRIDER, 2).build());
                        items.add(new ItemBuilder(Material.COOKED_FISH, 6).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 烤制 鲑鱼").build());
                        items.add(new ItemBuilder(Material.POTION, 3, (byte)5).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)2).setDisplayName(Squid.this.nameColor + Squid.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
    }
                }
                if (xyz.yuzegod.megawalls.util.ItemUtils.containsBow(items)) {
                    return items;
                }else {
                    items.add(new ItemBuilder(Material.BOW).setDisplayName(nameColor+getDisplayName()+" 弓").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY,10).build());
                    return items;
                }
            }
            
            @Override
            public List<String> getInfo(final int level) {
                final List<String> lore = new ArrayList<String>();
                switch (level) {
                    case 1: {
                        lore.add(" §8▪ §7铁 剑");
                        lore.add("    §8▪ 耐久 X");
                        lore.add(" §8▪ §7铁 靴子");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 保护 I");
                        lore.add(" §8▪ §7治疗药水 8❤");
                        lore.add(" §8▪ §7烤制 鲑鱼 x2");
                        break;
                    }
                    case 2: {
                        lore.add(" §8▪ §7铁 靴子");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 保护 I ➜ §aII");
                        lore.add(" §8▪ §7治疗药水 8❤ §8x1 ➜ §ax2");
                        lore.add(" §8▪ §7烤制 鱼 §8x2 ➜ §ax4");
                        break;
                    }
                    case 3: {
                        lore.add(" §8▪ §7铁 靴子");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 保护 II ➜ §aIII");
                        lore.add(" §a+ §7速度药水 II");
                        lore.add("    §8▪ 0:15");
                        break;
                    }
                    case 4: {
                        lore.add(" §8▪ §7铁 靴子");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 保护 III ➜ §aIV");
                        lore.add("    §a+ 深海探索者 I");
                        lore.add(" §8▪ §7铁 靴子");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §a+ 深海探索者 II");
                        lore.add("    §8▪ 保护 I ➜ §aII");
                        lore.add(" §8▪ §7治疗药水 8❤ §8x2 ➜ §ax3");
                        lore.add(" §8▪ §7烤制 鱼 §8x4 ➜ §ax6");
                        break;
                    }
                    case 5: {
                        lore.add(" §8▪ §7铁 §8➜ §a钻石 §7靴子");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 保护 IV ➜ §aIII");
                        lore.add("    §8▪ 深海探索者 I ➜ §aII");
                        break;
                    }
                }
                return lore;
            }
        });
    }
    
    @Override
    public List<String> getInfo() {
        final List<String> lore = new ArrayList<String>();
        lore.add("§7你想要在掌控局面的同时造成大量伤害.");
        lore.add("§7你喜欢在此之中灌输无尽的恐惧.");
        return lore;
    }
    
    @Override
    public int unlockCost() {
        return 5000;
    }
    
    @Override
    public int energyMelee() {
        return 10;
    }
    
    @Override
    public int energyBow() {
        return 10;
    }
    
    @Override
    public String getSkillTip(final GamePlayer gamePlayer) {
        return this.mainSkill.getSkillTip(gamePlayer);
    }
    
    @Override
    public void run() {
        GamePlayer.getOnlinePlayers().stream().filter(gamePlayer -> !gamePlayer.isSpectator() && ClassesManager.getSelected(gamePlayer).equals(this)).forEach(gamePlayer -> {
            if (Squid.skillCooldown.getOrDefault(gamePlayer, 0) > 0) {
                Squid.skillCooldown.put(gamePlayer, Squid.skillCooldown.get(gamePlayer) - 1);
            }
            if (Squid.skill3Cooldown.getOrDefault(gamePlayer, 0) > 0) {
                Squid.skill3Cooldown.put(gamePlayer, Squid.skill3Cooldown.get(gamePlayer) - 1);
            }
            gamePlayer.sendActionBar(this.getSkillTip(gamePlayer));
        });
    }
    
    @EventHandler(priority = EventPriority.LOWEST)
    public void onPlayerItemConsume(final PlayerItemConsumeEvent e) {
        if (e.getItem().getType() == Material.POTION) {
            final GamePlayer gamePlayer = GamePlayer.get(e.getPlayer().getUniqueId());
            final Classes classes = ClassesManager.getSelected(gamePlayer);
            if (classes.equals(this)) {
                this.secondSkill.use(gamePlayer, gamePlayer.getPlayerStats().getKitStats(classes));
            }
        }
    }
    
    @EventHandler(priority = EventPriority.LOWEST)
    public void onEntityDamage(final EntityDamageEvent e) {
        if (CitizensAPI.getNPCRegistry().isNPC(e.getEntity())) {
            return;
        }
        if (e.isCancelled() || !(e.getEntity() instanceof Player)) {
            return;
        }
        final GamePlayer gamePlayer = GamePlayer.get(e.getEntity().getUniqueId());
        final Classes classes = ClassesManager.getSelected(gamePlayer);
        if (classes.equals(this)) {
            final Player player = (Player)e.getEntity();
            if (player.getHealth() - e.getFinalDamage() < 21.0) {
                this.thirdSkill.use(gamePlayer, gamePlayer.getPlayerStats().getKitStats(ClassesManager.getSelected(gamePlayer)));
            }
        }
    }
    
    static {
        skillCooldown = new HashMap<GamePlayer, Integer>();
        skill3Cooldown = new HashMap<GamePlayer, Integer>();
    }
}
